From de452faa701939225b76d4344a3d04748f03d571 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Sat, 17 Feb 2024 10:24:15 +0700
Subject: Fix product variant api price

---
 src-migrate/pages/api/product-variant/[id].tsx | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

(limited to 'src-migrate/pages/api/product-variant/[id].tsx')

diff --git a/src-migrate/pages/api/product-variant/[id].tsx b/src-migrate/pages/api/product-variant/[id].tsx
index c25c10ac..39a871b7 100644
--- a/src-migrate/pages/api/product-variant/[id].tsx
+++ b/src-migrate/pages/api/product-variant/[id].tsx
@@ -1,3 +1,4 @@
+import moment from "moment";
 import { NextApiRequest, NextApiResponse } from "next";
 import { SolrResponse } from "~/types/solr";
 
@@ -28,6 +29,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
 
 const map = async (variant: any, price_tier: string) => {
   const data: any = {}
+  const price = variant[`price_${price_tier}_v2_f`] || 0
+  const isFlashsale = checkIsFlashsale(variant)
 
   data.id = parseInt(variant.id)
   data.parent_id = variant.template_id_i
@@ -36,10 +39,20 @@ const map = async (variant: any, price_tier: string) => {
   data.name = variant.name_s
   data.default_code = variant.default_code_s
   data.price = {
-    price: variant.price_v2_f,
-    discount_percentage: variant[`discount_${price_tier}_v2_f`] || 0,
-    price_discount: variant[`price_${price_tier}_v2_f`] || 0,
+    discount_percentage: isFlashsale ? variant.flashsale_discount_f : 0,
+    price: isFlashsale ? Math.round(variant.flashsale_base_price_f) : price,
+    price_discount: isFlashsale ? Math.round(variant.flashsale_price_f) : price
   }
 
   return data
+}
+
+const checkIsFlashsale = (variant: any) => {
+  const endDateStr = variant.flashsale_end_date_s || null
+  if (!endDateStr) return false
+
+  const now = moment()
+  const endDate = moment(endDateStr, 'YYYY-MM-DD HH:mm:ss')
+
+  return variant.flashsale_id_i > 0 && now.isSameOrBefore(endDate)
 }
\ No newline at end of file
-- 
cgit v1.2.3


From 2c4bd8d5640579222dc84e0026877fa92c445393 Mon Sep 17 00:00:00 2001
From: Rafi Zadanly <zadanlyr@gmail.com>
Date: Mon, 19 Feb 2024 13:16:33 +0700
Subject: Update variant api, ignore flashsale price

---
 src-migrate/pages/api/product-variant/[id].tsx | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

(limited to 'src-migrate/pages/api/product-variant/[id].tsx')

diff --git a/src-migrate/pages/api/product-variant/[id].tsx b/src-migrate/pages/api/product-variant/[id].tsx
index 39a871b7..955fde6a 100644
--- a/src-migrate/pages/api/product-variant/[id].tsx
+++ b/src-migrate/pages/api/product-variant/[id].tsx
@@ -30,7 +30,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
 const map = async (variant: any, price_tier: string) => {
   const data: any = {}
   const price = variant[`price_${price_tier}_v2_f`] || 0
-  const isFlashsale = checkIsFlashsale(variant)
 
   data.id = parseInt(variant.id)
   data.parent_id = variant.template_id_i
@@ -38,11 +37,7 @@ const map = async (variant: any, price_tier: string) => {
   data.image = variant.image_s
   data.name = variant.name_s
   data.default_code = variant.default_code_s
-  data.price = {
-    discount_percentage: isFlashsale ? variant.flashsale_discount_f : 0,
-    price: isFlashsale ? Math.round(variant.flashsale_base_price_f) : price,
-    price_discount: isFlashsale ? Math.round(variant.flashsale_price_f) : price
-  }
+  data.price = { discount_percentage: 0, price, price_discount: price }
 
   return data
 }
-- 
cgit v1.2.3

